//
//  Primer_TableVC.swift
//  APSwift
//
//  Created by AbnerPei on 2021/11/8.
//

import UIKit

class Primer_TableVC: UIViewController {
    
    lazy var tableView: UITableView = {
        let tableV = UITableView(frame: view.bounds, style: .plain)
        tableV.delegate = self
        tableV.dataSource = self
        tableV.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tableV
    }()
    
    var cellTitles: [String] = []
    var cellVCNames: [String] = []
    
    deinit {
//        debugPrint("123456 - Primer_TableVC deinit")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        debugPrint("123456 - Primer_TableVC viewWillDisappear")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        
        configDatas()
        view.addSubview(tableView)
    }
    
    func configDatas() {
        cellTitles = [
            "swift入门 - 命名空间（namespace）",
            "Swift入门 - 枚举（Enumerations）",
            "Swift入门 - 特性（Attributes）",
            "Swift入门 - 关键字",
            "Swift入门 - 类型擦除（Type Erasure）",
            "Swift入门 - 字符串",
            "Swift入门 - 控制流（control flow）",
            "Swift入门 - 区间类型（range）",
            "Swift入门 - 函数（functions）",
            "Swift入门 - 属性（properties）",
            "Swift入门 - 数组",
            "Swift入门 - Float",
            "Swift入门 - 多线程（Multithreading）",
            "Swift入门 - 协议（protocol）",
            "Swift入门 - 泛型（Generics）",
            "Swift入门 - 闭包（closure）",
            "Swift入门 - 构造过程（Intialization）",
            "Swift入门 - 错误处理（Error handling）",
            "Swift入门 - 特性",
            "Swift入门 - 声明（Declarations）"
        ]
        
        cellVCNames = [
            "Primer_Namespace_DemoVC",
            "Primer_Enumerations_DemoVC",
            "Primer_Attributes_TableVC",
            "Primer_Keyword_TableVC",
            "Primer_TypeErasure_DemoVC",
            "Primer_String_TableVC",
            "Primer_ControlFlow_DemoVC",
            "Primer_Range_DemoVC",
            "Primer_Functions_TableVC",
            "Primer_Properties_DemoVC",
            "Primer_Array_DemoVC",
            "Primer_Float_DemoVC",
            "Primer_Multithreading_TableVC",
            "Primer_Protocol_TableVC",
            "Primer_Generics_DemoVC",
            "Primer_Closure_TableVC",
            "Primer_Intialization_DemoVC",
            "Primer_ErrorHandling_DemoVC",
            "Primer_Attributes_TableVC",
            "Primer_Declarations_TableVC"
        ]
    }
    
}

extension Primer_TableVC: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellTitles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = cellTitles[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        pushToNext(vcTitles: cellTitles, vcNames: cellVCNames, at: indexPath)
    }
}
